<?php

namespace Database\Seeders;

use App\Models\Scm\Community;
use App\Models\Scm\Company;
use App\Models\Scm\Org;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class JtwyScmOrgInitSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $initData = [
            [
                'name' => '朝阳金泰物业管理有限责任公司',
                'relation_class' => Company::class,
                'parent_id' => 0,
                'children' => [
                    [
                        'name' => '金泰生活馆',
                        'relation_class' => Community::class,
                        'status' => 2,
                    ],
                    [
                        'name' => '壹品人家',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '金泰优家',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '壹品新院',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '中央公园',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '金泰春天',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '部队大院',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '金泰学府',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '朝阳学府',
                        'relation_class' => Community::class,
                    ],
                    [
                        'name' => '凤凰山云麓',
                        'relation_class' => Community::class,
                    ],
                ],
            ],
        ];
        DB::table('scm_orgs')->truncate();
        DB::table('scm_companies')->truncate();
        DB::table('scm_communities')->truncate();
        DB::table('scm_buildings')->truncate();

        foreach ($initData as $data) {
            $rootModel = new Org();
            $rootModel->name = $data['name'];
            $rootModel->relation_class = $data['relation_class'];
            $rootModel->parent_id = 0;
            if ($rootModel->save()) {
                $rootModel->refreshRelationClassInstance();
                foreach ($data['children'] as $child) {
                    $childModel = new Org();
                    $childModel->name = $child['name'];
                    $childModel->relation_class = $child['relation_class'];
                    $childModel->parent_id = $rootModel->id;
                    if ($childModel->save()) {
                        $childModel->refreshPaths();
                        $childModel->refreshRelationClassInstance();
                    }
                }
                $rootModel->refreshPaths();
            }
        }
        Org::extendSaveOrder(0);
    }
}
